﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Core.Framework.Model.Common;
using Core.Framework.Model.ViewModel;
using Core.Framework.Model.WebSockets;
using Core.Framework.Redis;
using Core.Framework.Redis.Queue_Helper;
using Core.Framework.Util;
using Core.Middleware.WebSockets;
using Core.WebSocketApi.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace Core.WebSocketApi.Controllers.Queue
{
    [Route(".v1/queue/")]
    [ApiController]
    public class QueueController : BaseApiController
    {
        /// <summary>
        /// 发布任务信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost(".push")]
        public IResult Push(QueryMessage model)
        {
            if (string.IsNullOrWhiteSpace(model.Token))
                model.Token = Guid.NewGuid().ToString();

            // 是否发布成功
            bool isPublish = new QueueMsg().DoDispatch(model);

            if (isPublish)
                return new ApiResult { code = CodeResult.SUCCESS, date = model.Token };
            else
                return new ApiResult { code = CodeResult.USER_NOT_LOGGED_IN, date = model.Token };

        }

        /// <summary>
        /// 调试专用 发布任务信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost(".testpush")]
        public IResult TestPush(QueryMessageTest model)
        {

            if (model.SystemToken == CoreStartupHelper.GetConfigValue("Service:System"))
            {
                if (string.IsNullOrWhiteSpace(model.Token))
                    model.Token = Guid.NewGuid().ToString();

                // 是否发布成功
                bool isPublish = new QueueMsg().DoDispatchTest(model);

                if (isPublish)
                    return new ApiResult { code = CodeResult.SUCCESS, date = model.Token };
                else
                    return new ApiResult { code = CodeResult.USER_NOT_LOGGED_IN, date = model.Token };
            }
            else {
                return new ApiResult { code = CodeResult.PERMISSION_NO_ACCESS, date = model.Token };
            }



        }

        /// <summary>
        /// 收到任务确认
        /// </summary>
        /// <returns></returns>
        [HttpPost(".Confirm")]
        public IResult Confirm(QueueRequest model)
        {

            if (!string.IsNullOrWhiteSpace(model?.Token))
            {
                this.iLog.Queue<QueueController>(new
                {
                    Client = CoreStartupHelper.GetConfigValue("Service")
                }.TryToJson(), "confirm", model.Token);

                return new ApiResult { code = CodeResult.SUCCESS, date = model };
            }
            else
                return new ApiResult { code = CodeResult.PARAMS_IS_NULL, date = model };

        }

        /// <summary>
        /// 任务已完成
        /// </summary>
        /// <returns></returns>
        [HttpPost(".Finished")]
        public IResult Finished(QueueRequest model)
        {

            if (!string.IsNullOrWhiteSpace(model?.Token))
            {
                this.iLog.Queue<QueueController>(new
                {
                    Client = CoreStartupHelper.GetConfigValue("Service")
                }.TryToJson(), "finished", model?.Token);

                return new ApiResult { code = CodeResult.SUCCESS, date = model };
            }
            else
                return new ApiResult { code = CodeResult.PARAMS_IS_NULL, date = model };

        }

    }
}
